package cn.gdk.gaodekecc.web.controller;

import cn.gdk.gaodekecc.service.IMovieTicketService;
import cn.gdk.gaodekecc.domain.MovieTicket;
import cn.gdk.gaodekecc.query.MovieTicketQuery;
import cn.gdk.gaodekecc.result.JSONResult;
import cn.gdk.gaodekecc.result.PageList;
import com.baomidou.mybatisplus.plugins.Page;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

@RestController
@RequestMapping("/movieTicket")
public class MovieTicketController {

    @Autowired
    public IMovieTicketService movieTicketService;

    /**
    * 保存和修改公用的
    */
    @RequestMapping(value="/save",method= RequestMethod.POST)
    public JSONResult saveOrUpdate(@RequestBody MovieTicket movieTicket){
        if(movieTicket.getId()!=null){
            movieTicketService.updateById(movieTicket);
        }else{
            movieTicketService.insert(movieTicket);
        }
        return JSONResult.success();
    }

    /**
    * 删除对象
    */
    @RequestMapping(value="/{id}",method=RequestMethod.DELETE)
    public JSONResult delete(@PathVariable("id") Long id){
        movieTicketService.deleteById(id);
        return JSONResult.success();
    }

    /**
   * 获取对象
   */
    @RequestMapping(value = "/{id}",method = RequestMethod.GET)
    public JSONResult get(@PathVariable("id")Long id){
        return JSONResult.success(movieTicketService.selectById(id));
    }


    /**
    * 查询所有对象
    */
    @RequestMapping(value = "/list",method = RequestMethod.GET)
    public JSONResult list(){
        return JSONResult.success(movieTicketService.selectList(null));
    }


    /**
    * 带条件分页查询数据
    */
    @RequestMapping(value = "/pagelist",method = RequestMethod.POST)
    public JSONResult page(@RequestBody MovieTicketQuery query){
        Page<MovieTicket> page = new Page<MovieTicket>(query.getPage(),query.getRows());
        page = movieTicketService.selectPage(page);
        return JSONResult.success(new PageList<MovieTicket>(page.getTotal(),page.getRecords()));
    }
    /**
     * 电影票购买接口
     * @param
     * @return
     */
    @RequestMapping(value = "/buymovie/{movieId}/{count}",method = RequestMethod.POST)
    public JSONResult buymovie(@PathVariable Long movieId,@PathVariable Integer count ){
        return movieTicketService.buy(movieId,count);
    }
}
